<template>
  <div class="login">
    <div class="login-form">
      <h4 class="login-title">登 录</h4>
      <el-form :model="login"
               :rules="loginForm"
               ref="loginForm"
               class="login-ruleForm">
        <el-form-item prop="username">
          <i class="el-icon-user username-icon"></i>
          <el-input v-model="login.username"
                    autocomplete="off"
                    class="username-ipt"
                    placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <i class="el-icon-lock password-icon"></i>
          <el-input type="password"
                    v-model="login.password"
                    autocomplete="off"
                    class="password-ipt"
                    placeholder="请输入密码"
                    show-password
                    @keyup.enter.native="handleLogin('loginForm')"></el-input>
        </el-form-item>
        <el-form-item class="login-btn-row">
          <el-button :loading="loading"
                     type="primary"
                     @click="handleLogin('loginForm')"
                     class="login-btn">登 录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from '@/api'
export default {
  data() {
    return {
      login: {
        username: '',
        password: ''
      },
      loginForm: {
        username: [{ required: true, message: '用户名不能为空' }],
        password: [{ required: true, message: '密码不能为空' }]
      },
      loading: false
    }
  },

  methods: {
    // 登录
    handleLogin() {
      this.$refs['loginForm'].validate(valid => {
        if (valid) {
          this.loading = true
          const params = {
            name: this.login.username,
            password: this.login.password
          }
          login(params)
            .then(res => {
              this.loading = false
              localStorage.setItem('ACCESS_TOKEN', res.data.token)
              this.$notify({
                title: '成功',
                message: '登录成功',
                type: 'success'
              })
              this.$router.push('/')
            })
            .catch(() => {
              this.loading = false
            })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import '~@/styles/variables.scss';

.login {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100vw;
  height: 100vh;
  background: $primary;
  .login-form {
    width: 420px;
    min-height: 300px;
    padding: 20px;
    background: $white;
    border-radius: 10px;
    box-shadow: $shadow;
    text-align: center;
    box-sizing: border-box;
    .login-title {
      font-size: 20px;
      font-weight: 700;
      padding-bottom: 32px;
    }
  }
  .username-icon,
  .password-icon {
    position: absolute;
    left: 10px;
    top: 10px;
    font-size: 20px;
  }
  .username-ipt,
  .password-ipt {
    box-sizing: border-box;
    padding: 2px 2px 2px 40px;
    border: 1px solid $border_color;
    border-radius: 4px;
  }
}
</style>

<style lang="scss">
.login {
  .login-form {
    .el-form-item__error {
      padding-top: 5px;
    }
    .el-form-item {
      margin-bottom: 30px;
      position: relative;
      .el-input__inner {
        border: 0 none;
        padding: 0;
      }
    }
    .login-btn-row {
      padding-top: 20px;
      .login-btn {
        width: 100%;
      }
    }
  }
}
</style>
